package devcontainer

import (
	"code.gitea.io/gitea/modules/log"
	DevcontainersVO "code.gitea.io/gitea/routers/api/devcontainer/vo"
	gitea_web_context "code.gitea.io/gitea/services/context"
	DevcontainersService "code.gitea.io/gitea/services/devstar_devcontainer"
)

// DeleteRepoDevContainerForCurrentActor 删除仓库 当前用户 Dev Container
func DeleteRepoDevContainerForCurrentActor(ctx *gitea_web_context.Context) {

	if isUserDevcontainerAlreadyInRepository(ctx) {
		opts := &DevcontainersVO.RepoDevcontainerOptions{
			Actor:      ctx.Doer,
			Repository: ctx.Repo.Repository,
		}
		err := DevcontainersService.DeleteRepoDevcontainer(ctx, opts)
		if err != nil {
			log.Warn("failed to delete devContainer with option{%v}: %v", opts, err.Error())
			ctx.Flash.Error(ctx.Tr("repo.dev_container_control.deletion_failed_for_user", ctx.Doer.Name))
		} else {
			ctx.Flash.Success(ctx.Tr("repo.dev_container_control.deletion_success_for_user", ctx.Doer.Name))
		}
	}
	ctx.JSONRedirect(ctx.Repo.RepoLink + "/dev-container")
}
